﻿using YKPrintService.VO;
using YKPrintService.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace YKPrintService.Service
{
    /// <summary>
    /// 云打印平台服务接口封装
    /// </summary>
    public class PrintService
    {
        private static readonly String BASE_URL = "https://open.xpyun.net";

        /// <summary>
        /// 1.批量添加打印机
        /// </summary>
        /// <param name="restRequest"></param>
        /// <returns></returns>
        public ObjectRestResponse<PrinterResult> addPrinters(AddPrinterRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/addPrinters";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<PrinterResult> result = JsonHelper.Json2Object<ObjectRestResponse<PrinterResult>>(resp);
            return result;
        }

        /**
     * 2.设置打印机语音类型
     * @param restRequest
     * @return
     */
        public ObjectRestResponse<bool> setPrinterVoiceType(SetVoiceTypeRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/setVoiceType";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<bool> result = JsonHelper.Json2Object<ObjectRestResponse<bool>>(resp);
            return result;
        }

        /**
         * 3.打印小票订单
         * @param restRequest - 打印订单信息
         * @return
         */
        public ObjectRestResponse<string> print(PrintRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/print";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<string> result = JsonHelper.Json2Object<ObjectRestResponse<string>>(resp);
            return result;
        }

        /**
         * 3.打印标签订单
         * @param restRequest - 打印订单信息
         * @return
         */
        public ObjectRestResponse<string> printLabel(PrintRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/printLabel";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<string> result = JsonHelper.Json2Object<ObjectRestResponse<string>>(resp);
            return result;
        }

        /**
         * 4.批量删除打印机
         * @param restRequest
         * @return
         */
        public ObjectRestResponse<PrinterResult> delPrinters(DelPrinterRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/delPrinters";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<PrinterResult> result = JsonHelper.Json2Object<ObjectRestResponse<PrinterResult>>(resp);
            return result;
        }

        /**
         * 5.修改打印机信息
         * @param restRequest
         * @return
         */
        public ObjectRestResponse<Boolean> updPrinter(UpdPrinterRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/updPrinter";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<bool> result = JsonHelper.Json2Object<ObjectRestResponse<bool>>(resp);
            return result;
        }

        /**
         * 6.清空待打印队列
         * @param restRequest
         * @return
         */
        public ObjectRestResponse<bool> delPrinterQueue(PrinterRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/delPrinterQueue";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<bool> result = JsonHelper.Json2Object<ObjectRestResponse<bool>>(resp);
            return result;
        }

        /**
         * 7.查询订单是否打印成功
         * @param restRequest
         * @return
         */
        public ObjectRestResponse<bool> queryOrderState(QueryOrderStateRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/queryOrderState";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<bool> result = JsonHelper.Json2Object<ObjectRestResponse<bool>>(resp);
            return result;
        }

        /**
         * 8.查询打印机某天的订单统计数
         * @param restRequest
         * @return
         */
        public ObjectRestResponse<OrderStatisResult> queryOrderStatis(QueryOrderStatisRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/queryOrderStatis";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<OrderStatisResult> result = JsonHelper.Json2Object<ObjectRestResponse<OrderStatisResult>>(resp);
            return result;
        }


        /**
         * 9.查询打印机状态
         *
         * 0、离线 1、在线正常 2、在线不正常
         * 备注：异常一般是无纸，离线的判断是打印机与服务器失去联系超过30秒
         * @param restRequest
         * @return
         */
        public ObjectRestResponse<int> queryPrinterStatus(PrinterRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/queryPrinterStatus";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<int> result = JsonHelper.Json2Object<ObjectRestResponse<int>>(resp);
            return result;
        }

        /// <summary>
        /// 10.批量查询打印机状态
        ///
        /// 0、离线 1、在线正常 2、在线不正常
        /// 备注：异常一般是无纸，离线的判断是打印机与服务器失去联系超过30秒
        /// </summary>
        /// <param name="restRequest"></param>
        /// <returns></returns>
        public ObjectRestResponse<List<int>> queryPrintersStatus(PrintersRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/queryPrintersStatus";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<List<int>> result = JsonHelper.Json2Object<ObjectRestResponse<List<int>>>(resp);
            return result;
        }

        /// <summary>
        /// 10.云喇叭播放语音
        /// </summary>
        /// <param name="restRequest"></param>
        /// <returns></returns>
        public ObjectRestResponse<string> playVoice(VoiceRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/playVoice";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<string> result = JsonHelper.Json2Object<ObjectRestResponse<string>>(resp);
            return result;
        }

        /// <summary>
        /// 12.POS指令
        /// </summary>
        /// <param name="restRequest">POS指令信息</param>
        /// <returns></returns>
        public ObjectRestResponse<string> pos(PrintRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/pos";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<string> result = JsonHelper.Json2Object<ObjectRestResponse<string>>(resp);
            return result;
        }

        /// <summary>
        /// 13. 钱箱控制
        /// </summary>
        /// <param name="restRequest"></param>
        /// <returns></returns>
        public ObjectRestResponse<String> controlBox(PrintRequest restRequest)
        {
            string url = BASE_URL + "/api/openapi/xprinter/controlBox";
            string jsonRequest = JsonHelper.Object2Json(restRequest);
            HttpHelper httpHelper = new HttpHelper();
            string resp = httpHelper.HttpPostJSON(url, jsonRequest);
            ObjectRestResponse<string> result = JsonHelper.Json2Object<ObjectRestResponse<string>>(resp);
            return result;
        }
    }
}
